﻿Imports System.Net.Sockets
Imports System.IO
Imports System.Text
Imports System.Threading
Imports System.Data.SQLite
Public Class Form1
    Private hostname As String = "fs.taricorp.net"
    Private port As Integer = 6600
    Private sw As StreamWriter
    Private sr As StreamReader
    Private ns As NetworkStream
    Private MPDSock As New TcpClient()
    Private SQLconnect As New SQLite.SQLiteConnection()
    Private SQLcommand As SQLiteCommand
    Private Delegate Function addItemCallback(ByRef item As String) As Boolean
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Function addItem(ByRef item As String) As Boolean
        If lstSongs.InvokeRequired() Then
            Dim d As addItemCallback = New addItemCallback(AddressOf addItem)
            Me.Invoke(d, New Object() {[item]})
        Else
            lstSongs.Items.Add(item)
        End If
    End Function

    Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectToolStripMenuItem.Click
        Dim fi As New FileInfo("temp.sd3")
        If fi.Exists = True Then
            fi.Delete()
        End If
        MPDSock.Connect(hostname, port)
        ns = MPDSock.GetStream
        sw = New StreamWriter(ns)
        sr = New StreamReader(ns)
        If sr.ReadLine().StartsWith("OK") = True Then
            lblStatus.Text = "Connected."
        End If
        SQLconnect.ConnectionString = "Data Source=temp.sd3;"
        SQLconnect.Open()
        SQLcommand = SQLconnect.CreateCommand()
        SQLcommand.CommandText = "CREATE TABLE songs(id INTEGER PRIMARY KEY, title TEXT, album TEXT, artist TEXT, path TEXT);"
        SQLcommand.ExecuteNonQuery()
        Dim t As New Thread(AddressOf getSongs)
        t.Start()
    End Sub

    Private Function getSongs() As Boolean
        Dim title As String
        Dim album As String
        Dim artist As String
        Dim file As String
        lblStatus.Text = "Downloading database from server..."
        sw.WriteLine("listallinfo")
        sw.Flush()
        Dim tempdata As String = ""
        While tempdata.StartsWith("OK") = False And tempdata.StartsWith("ACK") = False
            title = ""
            album = ""
            artist = ""
            file = ""
            While title = "" Or album = "" Or artist = "" Or file = ""
                tempdata = sr.ReadLine()
                If tempdata.StartsWith("Title") Then
                    title = tempdata.Remove(0, 7)
                End If
                If tempdata.StartsWith("Artist") Then
                    artist = tempdata.Remove(0, 8)
                End If
                If tempdata.StartsWith("Album") Then
                    album = tempdata.Remove(0, 7)
                End If
                If tempdata.StartsWith("file") Then
                    file = tempdata.Remove(0, 6)
                End If
            End While
            SQLcommand.CommandText = "INSERT INTO songs (title, album, artist, path) VALUES (""" & title & """, """ & album & """, """ & artist & """, """ & file & """)"
            SQLcommand.ExecuteNonQuery()
        End While
        lblStatus.Text = "Song list loaded."
    End Function

    Private Sub DisconnectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectToolStripMenuItem.Click
        sw.WriteLine("close")
        sw.Flush()
        MPDSock.Close()
        lblStatus.Text = "Disconnected"
    End Sub
End Class
